package com.phpxiu.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.LinearLayout;

import com.phpxiu.app.adapter.holder.HomeFollowHistoryViewHolder;
import com.phpxiu.app.adapter.holder.HomeFollowLivingViewHolder;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.LiveHistoryContent;
import com.phpxiu.app.model.LiveRoomHost;
import com.phpxiu.app.model.event.OnShowHotFragment;
import com.phpxiu.app.model.list.HomeFollow;
import com.phpxiu.app.utils.KKYUtil;
import com.phpxiu.app.view.UIHistoryVideoPlayer;
import com.phpxiu.app.view.UserHome;
import com.phpxiu.app.view.fragment.HomeFollowFragment;
import com.tencent.bugly.imsdk.crashreport.CrashReport;

import java.lang.ref.WeakReference;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by WuJinZhou on 2016/5/6.
 */
public class HomeFollowListAdapter extends BaseAdapter<HomeFollow> implements View.OnClickListener {
    public final static String TAG = "HomeAttentionListAdapter";
    private WeakReference<HomeFollowFragment> callBackRef;
    private LinearLayout.LayoutParams mPicBoxParams;
    private int picBoxHeight = 0;
    private AbsListView.LayoutParams emptyLivingParam=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    private AbsListView.LayoutParams historyLabelParam=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    private String dateDistanceYMD;
    private String dateDaysSuffix;
    private String dateHoursSuffix;
    private String dateMinuteSuffix;
    private String dateDefaultSuffix;
    private String defaultIntro;
    public HomeFollowListAdapter(Context context, List<HomeFollow> data, int screenWidth) {
        super(context, data);
        initDateSuffix(context);
        defaultIntro=context.getString(R.string.default_intro_content);
        picBoxHeight = (screenWidth * 732) / 750;
        emptyLivingParam.height= KKYUtil.dip2px(context, 100);
        historyLabelParam.height= KKYUtil.dip2px(context, 40);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HomeFollow a = data.get(position);
        LiveRoomHost host = a.getHost();
        int type=a.getListViewType();
        if(type== HomeFollow.TYPE_EMPTY_LIVING){//没有正在直播的
            if (convertView == null
                    ||convertView.getId()==R.id.home_follow_living_item_view
                    ||convertView.getId()==R.id.home_follow_history_label_view
                    ||convertView.getId()==R.id.home_follow_history_item_view) {
                convertView = mInflater.inflate(R.layout.home_follow_no_living_view, null);
                convertView.findViewById(R.id.show_hot).setOnClickListener(this);
                convertView.setLayoutParams(emptyLivingParam);
            }
        }

        if(type== HomeFollow.TYPE_LIVING_ITEM){
            HomeFollowLivingViewHolder holder = null;
            if (convertView == null
                    ||convertView.getId()==R.id.home_follow_no_living_view
                    ||convertView.getId()==R.id.home_follow_history_label_view
                    ||convertView.getId()==R.id.home_follow_history_item_view) {
                convertView = mInflater.inflate(R.layout.home_follow_living_item_view, null);
                holder = new HomeFollowLivingViewHolder(convertView);
                if (mPicBoxParams == null) {
                    mPicBoxParams = (LinearLayout.LayoutParams) holder.picBox.getLayoutParams();
                    mPicBoxParams.height = picBoxHeight;
                }
                holder.picBox.setLayoutParams(mPicBoxParams);
                convertView.setTag(holder);
            }
            holder = (HomeFollowLivingViewHolder) convertView.getTag();
            try {
                String head = host.getAvatar();
                if (head != null && !"".equals(head)) {
                    holder.head.setImageURI(Uri.parse(head));
                }
                holder.head.setTag(host.getUid());
                holder.head.setOnClickListener(this);
                holder.nick.setText(host.getNickname());
                holder.position.setText(a.getLbs().getAddress());
                holder.online.setText(a.getOnlineCount());
                String cover = a.getCover();
                if (cover == null || "".equals(cover)) {
                    holder.pic.setImageURI(Uri.parse("res:///" + R.drawable.cover_background));
                } else {
                    holder.pic.setImageURI(Uri.parse(cover));
                }
                holder.pic.setTag(a);
                holder.pic.setOnClickListener(this);
                //此处应为话题或与微博相关
                if (a.getTitle() != null && !"".equals(a.getTitle())) {
                    if (holder.topic.getVisibility() != View.VISIBLE) {
                        holder.topic.setVisibility(View.VISIBLE);
                    }
                    holder.topic.setText(a.getTitle());
                } else {
                    if (holder.topic.getVisibility() == View.VISIBLE) {
                        holder.topic.setVisibility(View.INVISIBLE);
                    }
                }
                String t=a.getType();
                if("3".equals(t)){
                    holder.stateSign.setImageResource(R.mipmap.live_type_pay_white_icon);
                }else if("2".equals(t)){
                    holder.stateSign.setImageResource(R.mipmap.live_type_locked_white_icon);
                }else{
                    holder.stateSign.setImageResource(R.mipmap.live_type_public_white_icon);
                }
            } catch (Exception e) {
                KKYUtil.log(TAG, e.getMessage() + "");
                return null;
            }
        }

        //精彩回放标题项
        if(type== HomeFollow.TYPE_HISTORY_ITEM_LABEL){
            if (convertView == null
                    ||convertView.getId()==R.id.home_follow_living_item_view
                    ||convertView.getId()==R.id.home_follow_no_living_view
                    ||convertView.getId()==R.id.home_follow_history_item_view) {
                convertView = mInflater.inflate(R.layout.home_follow_history_label_view, null);
                convertView.setLayoutParams(historyLabelParam);
            }
        }

        //精彩回放项
        if(type== HomeFollow.TYPE_HISTORY_ITEM){
            try {
                HomeFollowHistoryViewHolder holder = null;
                if (convertView == null
                        ||convertView.getId()==R.id.home_follow_living_item_view
                        ||convertView.getId()==R.id.home_follow_no_living_view
                        ||convertView.getId()==R.id.home_follow_history_label_view) {
                    convertView = mInflater.inflate(R.layout.home_follow_history_item_view, null);
                    holder=new HomeFollowHistoryViewHolder(convertView);
                    convertView.setTag(holder);
                }
                holder=(HomeFollowHistoryViewHolder)convertView.getTag();
                holder.head.setImageURI(Uri.parse(host.getAvatar()));
                holder.nick.setText(host.getNickname());
                try {
                    long distance=Long.parseLong(a.getDate());
                    holder.timeView.setText(KKYUtil.timeDistance(distance, dateDistanceYMD, dateDaysSuffix, dateHoursSuffix, dateMinuteSuffix, dateDefaultSuffix));
                }catch (NumberFormatException e){
                    e.printStackTrace();
                }
                holder.online.setText(a.getViewed());
                if(host.getIntro()==null||"".equals(host.getIntro())){
                    holder.introView.setText(defaultIntro);
                }else{
                    holder.introView.setText(host.getIntro()+"");
                }
                convertView.setOnClickListener(this);
                convertView.setTag(a);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return convertView;
    }

    private void initDateSuffix(Context context){
        dateDistanceYMD=context.getString(R.string.date_distance_ymd);
        dateDaysSuffix=context.getString(R.string.date_distance_day_ago);
        dateHoursSuffix=context.getString(R.string.date_distance_hour_ago);
        dateMinuteSuffix=context.getString(R.string.date_distance_minute_ago);
        dateDefaultSuffix=context.getString(R.string.date_distance_default);
    }

    /**
     * 监听进入房间
     */
     public void setOnEnterRoomCallBack(HomeFollowFragment callBack) {
        if (callBack == null) {
            if (callBackRef != null) {
                callBackRef.clear();
                callBackRef = null;
            }
        } else {
            if (callBackRef != null) {
                callBackRef.clear();
            }
            callBackRef = new WeakReference<HomeFollowFragment>(callBack);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.avatar_view:
                if (callBackRef != null && callBackRef.get() != null) {
                    if(v.getTag()!=null&&!"".equals(v.getTag())){
                        Intent home=new Intent(context, UserHome.class);
                        home.putExtra(UserHome.EXTRA_PARAM_KEY_UID,v.getTag().toString());
                        context.startActivity(home);
                    }
                }
                break;
            case R.id.pic_view:
                if (callBackRef != null && callBackRef.get() != null&&v.getTag()!=null&&v.getTag() instanceof HomeFollow) {
                    callBackRef.get().onEnterRoom((HomeFollow) v.getTag());
                }
                break;
            case R.id.show_hot:
                EventBus.getDefault().post(new OnShowHotFragment());
                break;
            case R.id.home_follow_history_item_view:
                if(v.getTag()!=null&&v.getTag() instanceof HomeFollow){
                    Intent play=new Intent(context, UIHistoryVideoPlayer.class);
                    play.putExtra(UIHistoryVideoPlayer.LIVE_INFO,new LiveHistoryContent((HomeFollow)v.getTag()));
                    context.startActivity(play);
                }else{
                    CrashReport.postCatchedException(new Exception("点击关注列表回放项时异常"));
                }
                break;
            default:
                break;
        }
    }
}
